%To write the source current files 
% for cable computation
%latest date 29.9.5
MyInPath = 'e:\Manoj\ocean\Velocities\monthly\';
MyOutPath = 'e:\Manoj\ocean\Source\180x360\monthly\';

if isdir(MyInPath) == 0,
    display('In path does not exist');
    return;
end;
if isdir(MyOutPath) == 0,
    mkdir('e:\Manoj\ocean\','Source\180x360\monthly\');
end;

S = dir(MyInPath);

nfiles = length(S);
ndatafile=1;
for i = 1:nfiles,
    if length(S(i).name) == 8,
        FileName(ndatafile,:) = S(i).name;
        ndatafile = ndatafile+1;
    end;
end;
fp = fopen('projectfiles.txt','wt');
for i = 1:ndatafile-1,
% %for i = 1:1,
%     disp([MyInPath FileName(i,:)]);
%  fid = fopen([MyInPath FileName(i,:)],'r','b');
%  IC = fread(fid,[180,360],'float');
%  ICU = fread(fid,[180,360],'float');
%  ICV= fread(fid,[180,360],'float');
%  IU = fread(fid,[180,360],'float');
%  IV = fread(fid,[180,360],'float');
%  Bz = fread(fid,[180,360],'float');
%  fclose(fid);
%  
% CU = -IV.*Bz.*1e-9.*3.2;
% CV = -IU.*Bz.*1e-9.*3.2;
% CU = circshift(CU,[0,180]);
% CV = circshift(CV,[0,180]);
% CU = CU*1000; % Compression for e format 20 oct 2005
% CV = CV*1000; % This multiplication is compensated by scale factor in source file
fprintf(fp,'%s\n',['    4000.           ' FileName(i,1:end-3) ' 1x1_mantle_k3.model ' FileName(i,1:end-3) '.source' ' 1']);
% fid = fopen([MyOutPath FileName(i,1:end-3) '.source'],'wt');
% fid1 = fopen('e:\manoj\ocean\x3dg\header.txt','rt');
% %the following format for alexei's x3dg is tested ok 20 oct 05
%            for ii = 1:34,
%                 s=fgetl(fid1);
%                 fprintf(fid,'%s\n',s);
%             end;
%             s=fgetl(fid1);
% for in = 1:180,
%     for jn = 1:360,
%        fprintf(fid,'%12.4e\n',CU(in,jn));
%    end;
% end;
%             fprintf(fid,'\n');
%             for ii = 1:17,
%             s=fgetl(fid1);
%             fprintf(fid,'%s\n',s);
%             end;
%             s=fgetl(fid1);
% for in = 1:180,
%     for jn = 1:360,
%         fprintf(fid,'%12.4e\n',0);
%     end;
% end;
%             fprintf(fid,'\n');
%             for ii = 1:17,
%             s=fgetl(fid1);
%             fprintf(fid,'%s\n',s);
%             end;
%             s=fgetl(fid1);
%  for in = 1:180,
%     for jn = 1:360,       
%         fprintf(fid,'%12.4e\n',CV(in,jn));
%     end;
% end;
%             fprintf(fid,'\n');
%             for ii = 1:17,
%             s=fgetl(fid1);
%             fprintf(fid,'%s\n',s);
%             end;
%             s=fgetl(fid1);
%   
%  for in = 1:180,
%     for jn = 1:360,       
%         fprintf(fid,'%12.4e\n',0);
%     end;
% end;
%             fprintf(fid,'\n');
%             for ii = 1:37,
%             s=fgetl(fid1);
%             fprintf(fid,'%s\n',s);
%             end;
%             s=fgetl(fid1);
% 
%            fclose(fid);
%            fclose(fid1);
end;
fclose all;